package l1j.opqlo.NewSystem;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.L1Location;
import l1j.server.server.model.Instance.L1HummerInstance;
import l1j.server.server.serverpackets.S_EffectLocation;
import l1j.server.server.serverpackets.S_NPCPack;
import l1j.server.server.serverpackets.S_RemoveObject;
import l1j.server.server.templates.L1Npc;
import l1j.server.server.utils.Random;

public class PlayHamster {
	private static PlayHamster _instance;
	private static final ArrayList<L1HummerInstance> _mob = new ArrayList<L1HummerInstance>();
	private static final ArrayList<L1Location>_loc = new ArrayList<L1Location>();
	private static final HashSet<L1HummerInstance> _spwn = new HashSet<L1HummerInstance>();
	private static final HashSet<L1Location>_useLoc = new HashSet<L1Location>();
	public static PlayHamster getIntance(){
		if(_instance == null){
			_instance = new PlayHamster();
		}
		return _instance;
	}
	private ScheduledFuture<?> _timer;
	
	public PlayHamster(){
		setNpc();
		setLoc();
		new Game().start();
	}

	private void setNpc(){
		for(int i=710073; i<=710090; i++){
			L1Npc temp =NpcTable.getInstance().getTemplate(i);
			L1HummerInstance npc = new L1HummerInstance(temp);
			_mob.add(npc);
		}
		for(int i=0; i<10; i++){
			final L1Npc temp =NpcTable.getInstance().getTemplate(710091);
			final L1HummerInstance npc = new L1HummerInstance(temp);
			_mob.add(npc);
		}
//		for(int i=0; i<9; i++){
//			if(i==0){
//				L1Npc temp =NpcTable.getInstance().getTemplate(710074);
//				L1HummerInstance npc = new L1HummerInstance(temp);
//				npc.setId(IdFactory.getInstance().nextId());
//				L1World.getInstance().storeObject(npc);
//				_mob.add(npc);
//				npc.setHeading(6);
//				npc.setStatus(4);
//			}else{
//				L1Npc temp =NpcTable.getInstance().getTemplate(710073);
//				L1HummerInstance npc = new L1HummerInstance(temp);
//				npc.setId(IdFactory.getInstance().nextId());
//				L1World.getInstance().storeObject(npc);
//				_mob.add(npc);
//				npc.setHeading(6);
//				npc.setStatus(4);
//			}
//		}
	}
	
	private void setLoc(){
		_loc.add(new L1Location(32756 , 32830 , 1784));
		_loc.add(new L1Location(32756 , 32832 , 1784));
		_loc.add(new L1Location(32756 , 32834 , 1784));
		_loc.add(new L1Location(32754 , 32830 , 1784));
		_loc.add(new L1Location(32754 , 32832 , 1784));
		_loc.add(new L1Location(32754 , 32834 , 1784));
		_loc.add(new L1Location(32752 , 32830 , 1784));
		_loc.add(new L1Location(32752 , 32832 , 1784));
		_loc.add(new L1Location(32752 , 32834 , 1784));
	}







	private class Game extends TimerTask {

		public void start(){
			_timer = GeneralThreadPool.getInstance().scheduleAtFixedRate(this,3500, 3500);
		}
		@Override
		public void run() {
			try{
				_useLoc.clear();
				for(final L1HummerInstance npc : _spwn){
					L1World.getInstance().removeVisibleObject(npc);//移除地圖標記
					npc.broadcastPacketAll(new S_RemoveObject(npc));// 移除顯示
					Thread.sleep(10);
				}
				_spwn.clear();
				Thread.sleep(300);
				final int count =Random.nextInt(9)+1;
				
				while(_spwn.size() <count){
					
					L1HummerInstance npc = _mob.get(Random.nextInt(_mob.size()));
					boolean isSpwn =_spwn.add(npc);
					if(!isSpwn){
						continue;
					}
					 boolean ok = false;
					do{
						final L1Location loc = _loc.get(Random.nextInt(_loc.size()));
					ok =_useLoc.add(loc);
					if(ok){
						npc.setLocation(loc);
						L1World.getInstance().addVisibleObject(npc);
						npc.broadcastPacketX8(new S_EffectLocation(loc.getX() , loc.getY() , 5013));
						npc.broadcastPacketAll(new S_NPCPack(npc));
						npc.clearList();
//						npc.broadcastPacketX8(new S_SkillSound(npc.getId(), 4859));
						Thread.sleep(10);
						break;
					}
					}while(true);
				}
			}catch(Exception e){
				GeneralThreadPool.getInstance().cancel(_timer, false);
				Game game = new Game();
				game.start();
			}
		}
	}
}
